Imports System.ComponentModel
Imports System.Configuration.Provider
Imports System.Web.Configuration
Imports System.Web.Security

<DataObject(True)> _
Public Class RoleODS

    <DataObjectMethod(DataObjectMethodType.Select, True)> _
    Public Function GetRoles() As List(Of Role)
        Return GetRoles(Nothing, False)
    End Function

    <DataObjectMethod(DataObjectMethodType.Select, False)> _
    Public Function GetRoles(ByVal userName As String, _
    ByVal showOnlyAssignedRoles As Boolean) As List(Of Role)
        Dim roleList As New List(Of Role)
        Dim rolesArray() As String = Roles.GetAllRoles()
        For Each roleName As String In rolesArray
            Dim isUserInRole As Boolean = False
            If Not String.IsNullOrEmpty(userName) Then
                isUserInRole = Roles.IsUserInRole(userName, roleName)
            End If
            If showOnlyAssignedRoles = False OrElse isUserInRole = True Then
                ' Getting usersInRole is only used for the count below
                Dim usersInRole() As String = Roles.GetUsersInRole(roleName)
                Dim rd As New Role
                rd.RoleName = roleName
                rd.Description = MembershipsAndRolesODS.Provider.GetRoleDescription(roleName)
                rd.IsUserInRole = isUserInRole
                rd.NumberOfUsersInRole = usersInRole.Length
                roleList.Add(rd)
            End If
        Next
        Return roleList
    End Function

    <DataObjectMethod(DataObjectMethodType.Select, False)> _
    Public Function GetUsersInRole(ByVal roleName As String) As DataView
        If String.IsNullOrEmpty(roleName) Then
            Return Nothing
        End If
        Dim dt As New DataTable
        dt.Columns.Add("UsersInRole")
        For Each UserName As String In Roles.GetUsersInRole(roleName)
            Dim dr As DataRow = dt.NewRow
            dr(0) = UserName
            dt.Rows.Add(dr)
        Next
        Return dt.DefaultView
    End Function

    <DataObjectMethod(DataObjectMethodType.Insert, False)> _
    Public Sub InsertRole(ByVal roleName As String, ByVal desc As String)
        If Roles.RoleExists(roleName) Then
            Exit Sub
        End If
        If Not String.IsNullOrEmpty(desc) Then
            MembershipsAndRolesODS.Provider.UpdateRoleDescription(roleName, desc)
        End If
        Roles.CreateRole(roleName)
    End Sub

    <DataObjectMethod(DataObjectMethodType.Delete, False)> _
    Public Sub DeleteRole(ByVal roleName As String)
        Select Case roleName
            Case "QuestionnaireAdmins", "WebAdmins"
                Exit Sub
            Case Else
                Dim userNames As String() = Roles.GetUsersInRole(roleName)
                If userNames.Length > 0 Then
                    Roles.RemoveUsersFromRole(userNames, roleName)
                End If
                MembershipsAndRolesODS.Provider.DeleteRoleDescription(roleName)
                Roles.DeleteRole(roleName)
        End Select
    End Sub

    <DataObjectMethod(DataObjectMethodType.Update, False)> _
    Public Sub UpdateRole(ByVal roleName As String, _
    ByVal description As String, _
    ByVal NumberOfUsersInRole As Integer)
        MembershipsAndRolesODS.Provider.UpdateRoleDescription(roleName, description)
    End Sub
End Class
